// var productObj = require('product-model.js');

import { Product } from 'product-model.js';

import { Store } from '../store/store-model.js';
import { Subscribe } from '../subscribe/subscribe-model.js';
import { Follow } from '../follow/follow-model.js';
import { My } from '../my/my-model.js';
import { Config } from '../config/config-model.js';
var product = new Product();  //实例化 商品详情 对象
var store = new Store();
var subscribe = new Subscribe();
var follow = new Follow();
var my = new My();
var config = new Config();
Page({
  data: {
    loadingHidden: false,
    hiddenSmallImg: true,
    hiddenmodalput: true,
    countsArray: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    productCounts: 1,
    currentTabsIndex: 0,
    cartTotalCounts: 0,
    showTopTips: false,
    radioItems: [
      { name: '女士', value: '0' },
      { name: '男士', value: '1', checked: true }
    ],

    countryIndex: 0,
    countryIndexs: 0,
    date: "2016-09-01",
    res: '#fff',

    userRes: '#fff',
    countryCodes: ["+86", "+80", "+84", "+87"],
    countryCodeIndex: 0,

    isAgree: false,
    currentMenuIndex: 0,//初始化导航索引，从第0开始
    loadingHidden: false,
    showTopTips: false,
  },
  //点击按钮痰喘指定的hiddenmodalput弹出框  
  modalinput: function () {
    var that = this;
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var dates = myDate.getDate();
    wx.getSetting({
      success: (ress) => {
        // console.log(res.authSetting['scope.userInfo']);

        if (!ress.authSetting['scope.userInfo']) {
          wx.openSetting({
            success: (res) => {
              if (res.authSetting['scope.userInfo'] == true) {
                wx.redirectTo({
                  url: '../product/product?id=' + this.data.id
                })
              }
            }

          })
        } else {

          this.setData({
            hiddenmodalput: !this.data.hiddenmodalput,
            newTime: year + '-' + month + '-' + dates
          });
        }

      }
    })


  },
  //取消按钮  
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });

  },
  userNameInput: function (e) {


    this.setData({
      userName: e.detail.value,
    })


  },
  phoneInput: function (e) {
    var reg = new RegExp('^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$', 'g');

    var rsLowerCase = reg.exec(e.detail.value);
    if (rsLowerCase) {
      this.setData({
        phone: e.detail.value,
        res: 'green'
      })
      //   console.log(this.data.phone);
    } else {
      this.setData({
        res: 'red'
      })
      //   console.log(this.data.res);
    }

  },
  //确认  
  confirm: function () {

    //    var username =  this.data.userName;
    for (var i in this.data.radioItems) {

      if (this.data.radioItems[i].checked == true) {
        var sex = this.data.radioItems[i].value;
      }
    }


    var formData = new Array();
    formData['username'] = this.data.userName;
    formData['phone'] = this.data.phone;
    formData['sex'] = sex;

    formData['time'] = this.data.date;
    formData['goods_id'] = this.data.product['id'];
    var that = this;
    // console.log(formData);
    if (formData['username'] && formData['phone'] && formData['time']) {
      if (this.data.countryIndex == 0) {
        this.setData({
          showTopTips: true,
        });
        setTimeout(function () {

          that.setData({
            showTopTips: false,

          });


        }, 3000);
        this.setData({
          error: '请选择城市与门店'
        });
      } else {
        formData['city'] = this.data.storeTypeName[this.data.countryIndex];
        formData['city_name'] = this.data.storeInfoArr[this.data.countryIndexs];
        subscribe.setSubscribe(formData, (data) => {
          //    console.log(data);
          // if(data.pass) {

          this.setData({
            hiddenmodalput: true
          });
          wx.redirectTo({
            url: '../subscribe/subscribe',
            success: function (res) {
              var page = getCurrentPages().pop();
              if (page == undefined || page == null) return;
              page._loadData();
            }
          });
          // }
        });
      }
    } else {
      this.setData({
        showTopTips: true,
      });
      setTimeout(function () {

        that.setData({
          showTopTips: false,

        });


      }, 3000);
      this.setData({
        error: '请完善信息'
      });

    }


  },

  onLoad: function (option) {
    var id = option.id;

    this.data.id = id;
    this._loadData();
  },
  calling: function () {
    wx.makePhoneCall({
      phoneNumber: '400-188-6061',
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  /*加载所有数据*/
  _loadData: function (callback) {
    var that = this;
    product.getDetailInfo(this.data.id, (data) => {
      console.log(data);
      that.setData({

        product: data,
        loadingHidden: true
      });
      callback && callback();
    });


    config.getConfigData(this.data.id, (data) => {
      that.setData({
        configArr: data,
        loadingHidden: true
      });
      callback && callback();
    });
    wx.getSetting({
      success: (ress) => {
        // console.log(res.authSetting['scope.userInfo']);

        if (ress.authSetting['scope.userInfo']) {
          subscribe.getSubscribe(this.data.id, (data) => {
            // console.log(data);
            that.setData({
              subscribe: data
            })
          })
          store.getStoreCityName((getStoreCityName) => {
            // console.log(getStoreCityName);
            getStoreCityName.unshift('请选择城市');
            that.setData({
              storeTypeName: getStoreCityName,
              loadingHidden: true

            });
          });
          store.getStoreCityId((getStoreCityId) => {
            //   console.log(getStoreCityId);

            that.setData({
              storeTypeId: getStoreCityId,
              loadingHidden: true

            });
          });
          follow.getFollowData(this.data.id, (data) => {
            //   console.log(data);
            that.setData({
              followData: data,
              loadingHidden: true

            });
          })
        }

      }
    })


  },


  changeFollow: function (e) {
    wx.getSetting({
      success: (ress) => {
        // console.log(res.authSetting['scope.userInfo']);

        if (!ress.authSetting['scope.userInfo']) {
          wx.openSetting({
            success: (res) => {
              if (res.authSetting['scope.userInfo'] == true) {
                wx.redirectTo({
                  url: '../product/product?id=' + this.data.id
                })
              }
            }

          })
        } else {

          var id = this.data.id;
          if (e.currentTarget.dataset.state == '0') {
            //console.log(1);
            follow.setFollowData(id, (data) => {
              this._loadData();
            })
          } else {
            //console.log(0);
            follow.delFollowData(id, (data) => {
              this._loadData();
            })
          }
        }

      }
    })

  },
  //切换详情面板
  onTabsItemTap: function (event) {
    var index = product.getDataSet(event, 'index');
    if (index == 1) {

    }
    this.setData({
      currentTabsIndex: index
    });
  },








  /*下拉刷新页面*/
  onPullDownRefresh: function () {
    this._loadData(() => {
      wx.stopPullDownRefresh()
    });
  },

  //分享效果
  onShareAppMessage: function () {
    return {
      title: '爱车易得',
      path: 'pages/product/product?id=' + this.data.id,
      success: function () {
        wx.showToast({
          title: '已完成',
          icon: 'success',

        });
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  //性别
  radioChange: function (e) {
    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;

    }
    this.setData({
      radioItems: radioItems
    });
  },
  //时间
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },





  //根据城市选择门店
  bindCountryChange: function (e) {

    var map = this;
    var storeTypeId = map.data.storeTypeId;
    var id = storeTypeId[e.detail.value - 1];
    this.setData({
      countryIndex: e.detail.value
    });

    store.getStoreInfo(id, (getStoreInfo) => {


      this.setData({
        storeInfoArr: getStoreInfo,
        loadingHidden: true

      });
    });
  },

  previewImage: function (e) {
    var that = this,
      //获取当前图片的下表
      index = e.currentTarget.dataset.index,
      //数据源
      pictures = this.data.product.pictures;
    console.log(pictures)
    wx.previewImage({
      //当前显示下表
      current: pictures[index],
      //数据源
      urls: pictures
    })
  }

})


